package com.imedia.frame.lucene.utils;

import java.util.ResourceBundle;

/**
 * <p>Title: 全站检索</p>
 * <p>Description: 逻辑处理工具类</p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: </p>
 * @author qiuy
 * @version 1.0
 */
public class IndexUtils {
	private static ResourceBundle bundle = null;
	static {
		try {
			bundle = ResourceBundle.getBundle("lucene");//加载资源文件
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	/**
	 * <p>description: 过滤字符串 为空(null)时候 返回 "" 防止空指针异常</p>
	 * @param o
	 * @return
	 */
	public static String getString(Object o){
		String str = "";
		if(o != null){
			str = String.valueOf(o).trim();
		}
		return str;
	}
	/**
	 * <p>description: 得到资源文件信息</p>
	 * @param key
	 * @return
	 */
	public static String getMessage(String key){
		String rtnVal = "";
		try {
			rtnVal = bundle.getString(key);
		}
		catch(Exception ex) {
			rtnVal = "";
		}
		return rtnVal;
	}
	/**
	 *  <p>description: 得到int数组中最大的数</p>
	 * @param counts
	 * @return
	 */
	public static int  getMaxIntValue(int[] counts){
		int temp = 0;
		int max = counts[0];
		for (int i = 1; i < counts.length; i++) {
			if (counts[i] > max) {
				temp = max;
				max = counts[i];
				counts[i] = temp;
			}
		}
		return max;
	}
	/**
	 *  <p>description: 得到int数组中最小的数</p>
	 * @param counts
	 * @return
	 */
	public static int getMinIntValue(int[] counts){
		@SuppressWarnings("unused")
		int temp = 0;
		int min = counts[0];
		for (int i = 1; i < counts.length; i++) {
			if (counts[i] < min) {
				temp = min;
				min = counts[i];
				counts[i] = min;
			}
		}
		return min;
	}
	/**
	 * <p>description: 动态得到检索语句</p>
	 * @param kewwrods 关键字 若要搜索多个关键字用空格隔开 譬如我要搜 一条记录有A也有B 前台需传入 "A B"
	 * @param columns  搜索的域string数组类型
	 * @return
	 */
	public static StringBuffer getSearchStringBufferForOr(String keywords,String[] columns){
		StringBuffer querystr = new StringBuffer();
		if (keywords == null || "".equals(keywords) || columns == null || columns.length == 0) {
			return querystr;
		}
		String[] keys = keywords.split("\\s+");
		int columnsLength = columns.length;
		for (int i = 0; i < keys.length; i++) {
			for (int j = 0; j < columnsLength; j++) {
				if (j == 0) {
					querystr.append(" (").append(columns[j]).append(":").append(keys[i]);
				}else if (j < columnsLength - 1) {
					querystr.append(" ").append(columns[j]).append(":").append(keys[i]);
				}else {
					querystr.append(" ").append(columns[j]).append(":").append(keys[i]).append(")");
				}
			}
		}
		System.out.println(querystr.toString());
		return querystr;
	}
	/**
	 * <p>description: 动态得到检索语句</p>
	 * @param kewwrods 关键字 若要搜索多个关键字用空格隔开 譬如我要搜 一条记录有A或者B 前台需传入 "A B"
	 * @param columns  搜索的域string数组类型
	 * @return
	 */
	public static StringBuffer getSearchStringBufferForAnd(String keywords,String[] columns){
		StringBuffer querystr = new StringBuffer();
		if (keywords == null || "".equals(keywords) || columns == null || columns.length == 0) {
			return querystr;
		}
		String[] keys = keywords.split("\\s+");
		int columnsLength = columns.length;
		for (int i = 0; i < keys.length; i++) {
			for (int j = 0; j < columnsLength; j++) {
				if (j == 0) {
					querystr.append(" +(").append(columns[j]).append(":").append(keys[i]);
				}else if (j < columnsLength - 1) {
					querystr.append(" ").append(columns[j]).append(":").append(keys[i]);
				}else {
					querystr.append(" ").append(columns[j]).append(":").append(keys[i]).append(")");
				}
			}
		}
		System.out.println(querystr.toString());
		return querystr;
	}
	/**
	 * <p>description: 得到int型数据</p>
	 * @param str
	 * @return
	 */
	public static int getInt(String str){
		int n = 0;
		if(str != null && !str.equals("")){
			try {
				n = Integer.parseInt(str);
			} catch (Exception e) {
				n = 0;
				System.out.println("传递非数字型字符串！");
			}			
		}
		return n;
	}
	/**
	 * <p>description: 过滤字符串特殊字符</p>
	 * @param str
	 * @return
	 */
	public static boolean stringFilter(String str){
		//设定字典 用于搜索参数过滤参照
		String dictionary = "~`!@#$%^&*()=+\\|/;:'\"?><,.";
		String[] checkStrs = str.split("\\s+");
		boolean flag = false;
		for (int i = 0; i < checkStrs.length; i++) {
			flag = dictionary.indexOf(checkStrs[i]) != -1 ? true : false;
		}
		return flag;
	}
	/**
	 * <p>description: 字符转码通用方法</p>
	 * @param o
	 * @param encodingType
	 * @return
	 */
	public static String getString(Object object, String encodingType) {
		
		String resultStr = "";
		if(object != null){
			String value = String.valueOf(object);
			
			try {
				String valueTemp = new String(value.getBytes(encodingType), encodingType);
				if (!value.equals(valueTemp)) {
					value = new String(value.getBytes("ISO-8859-1"), encodingType);
				}
			} catch (Exception e) {
				System.out.println("encoding change Exception");
			}
			resultStr = value.trim();
		}
		System.out.println(resultStr);
		return resultStr;
	}
	/**
	 * <p>description: 得到数字类型String参数 过滤</p>
	 * @param o
	 * @return
	 */
	public static String checkStringIsInt(Object o) {
		String str = "";
		try {
			Integer.parseInt((o.toString()).trim());
		}
		catch (Exception e) {
			System.out.println("参数传递错误");
			return "";
		}
		if (o != null) {
			str = o.toString().trim();
		}
		else {
			str = "";
		}
		return str;
	}
	/**
	 * 测试main方法
	 * @param args
	 */
	public static void main(String[] args) {
		String keywords = "你好 我们 中国";
		String[] columns = {"title","author"}; 
		getSearchStringBufferForOr(keywords,columns);
	}
}
